package com.lmj.wx_blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmj.wx_blog.entry.User2;
import com.lmj.wx_blog.mapper.User2Mapper;
import com.lmj.wx_blog.service.User2Service;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author li290
 * @description 针对表【user2】的数据库操作Service实现
 * @createDate 2022-01-19 14:37:57
 */
@Service
public class User2ServiceImpl extends ServiceImpl<User2Mapper, User2>
        implements User2Service {
    @Autowired
    User2Mapper userMapper;
    @Autowired
    SqlSessionFactory sqlSessionFactory;

    @Transactional(rollbackFor = Exception.class)
    public void addUserOneByOne(List<User2> users) {
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
        User2Mapper um = session.getMapper(User2Mapper.class);
        long startTime = System.currentTimeMillis();
        for (User2 user : users) {
            um.addUserOneByOne(user);
        }
        session.commit();
        long endTime = System.currentTimeMillis();
        System.err.println("一条条插入 SQL 耗费时间 " + (endTime - startTime));
    }
}




